package com.gr.dmbackend.service.Impl.student;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gr.dmbackend.mapper.StudentMapper;
import com.gr.dmbackend.model.Student;
import com.gr.dmbackend.service.student.GetStudentInfoService;
import com.gr.dmbackend.utils.GetUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GetStudentInfoServiceImpl implements GetStudentInfoService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public JSONObject getStudentInfo() {

        JSONObject resp = new JSONObject();

        int userId = GetUserUtil.getCurrentUser().getId();

        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        Student student = studentMapper.selectOne(queryWrapper);

        if (student == null) {
            resp.put("error_message", "error");
            return resp;
        }

        resp.put("studentId", student.getStudentId());
        resp.put("name", student.getName());
        resp.put("gender", student.getGender());
        resp.put("grade", student.getGrade());
        resp.put("building", student.getBuilding());
        resp.put("dormId", student.getDormId());
        resp.put("error_message", "success");

        return resp;
    }
}
